package com.springBoot.controller;

import com.springBoot.commonClass.PostNewFile;
import com.springBoot.entity.ObjectsEntity;
import com.springBoot.exception.BusinessFailureException;
import com.springBoot.service.FileOperationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/file-operation")
public class FileOperationController {

    @Autowired
    public FileOperationService fileOptSvr;

    @PostMapping("/create-file")
    public PostNewFile createNewFile(@RequestBody PostNewFile postFile) throws ExecutionException, InterruptedException {
        PostNewFile newFile = new PostNewFile();
        System.out.println("-----------------" + postFile.getObjId());
        BeanUtils.copyProperties(postFile, newFile);
//        return fileOptSvr.insertNewFile(newFile);
        return fileOptSvr.insertNewFile(newFile).get();
    }

    @GetMapping("/query")
    public PostNewFile queryFile(ObjectsEntity obj) throws ExecutionException, InterruptedException {
        if(obj.getObjId() == null && obj.getOrgId() == null){
            throw new BusinessFailureException(true);
        }
        return fileOptSvr.querySingleFile(obj).get();
    }

    @GetMapping("/query-all-files")
    public List<ObjectsEntity> queryAllFiles(String orgId){
        return null;
    }
    @PostMapping("/delete-file")
    public void deleteFile(@RequestBody ObjectsEntity objectsEntity){
        this.fileOptSvr.deleteSingleFile(objectsEntity);
    }

    @PostMapping("/modify-file-name")
    public void modifyFileName(@RequestBody ObjectsEntity objectsEntity){
        this.fileOptSvr.modifyFileName(objectsEntity);
    }
}
